package com.macro.mall.service;


import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.IdWorker;
import com.macro.mall.mapper.SpsDataContentMapper;
import com.macro.mall.mapper.admin.BasicCollectionManMapper;
import com.macro.mall.mapper.admin.BasicUsersManMapper;
import com.macro.mall.mapper.admin.YuqingProDataMapper;
import com.macro.mall.model.SpsDataContent;
import com.macro.mall.model.YuqingProDataParam;
import com.macro.mall.model.admin.BasicUsersMan;
import com.macro.mall.model.admin.YuqingProData;
import com.macro.mall.query.YuqingProDataQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class YuqingProDataService {

    @Autowired
    private YuqingProDataMapper yuqingProDataMapper;

    @Autowired
    private SpsDataContentMapper contentMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BasicCollectionManMapper collectionManMapper;

    @Autowired
    private BasicUsersManMapper usersManMapper;



    /***
     * 数据初始化
     */
    public CommonResult init(YuqingProDataParam yuqingProDataParam) throws ParseException {
        Map<String, Object> result = new HashMap<>();
        YuqingProDataQuery query = new YuqingProDataQuery();
        BeanUtils.copyProperties(yuqingProDataParam, query);
        List<YuqingProData> yuqingProDataList = yuqingProDataMapper.selectList(query);

        //模拟cityId
        Integer[] arr = new Integer[]{3, 4, 5, 6, 7, 8};

        Integer[] arr1 = new Integer[]{24546, 435687, 345980, 23456, 234567, 76543};
        Integer[] arr2 = new Integer[]{867589, 546756, 988759, 45768, 234567, 765767};
        List<Integer> integers = Arrays.asList(arr);
        List<Integer> integers2 = Arrays.asList(arr1);
        List<Integer> integers3 = Arrays.asList(arr2);


        List<SpsDataContent> contentList = new ArrayList<>();
        List<BasicUsersMan> usersManList = new ArrayList<>();
        //SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false); //关闭事务
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (YuqingProData yuqingProData : yuqingProDataList) {

            String platformId = collectionManMapper.selectIdByPlatformName(yuqingProData.getMediaName());
            //===================用户管理
            long userId = idWorker.nextId();
            if (yuqingProData.getAuthor() != null && yuqingProData.getAuthor().length() > 0) {
                BasicUsersMan usersMan = new BasicUsersMan();
                usersMan.setId(String.valueOf(userId));
                usersMan.setSignId(String.valueOf(userId));
                if (yuqingProData.getAuthor() != null) {
                    usersMan.setUserName(yuqingProData.getAuthor());
                    usersMan.setNickName(yuqingProData.getAuthor());
                    if (platformId != null && platformId.length() > 0) {
                        usersMan.setPortraitLabel(platformId);
                    } else {
                        usersMan.setPlatform(1L);
                    }
                    usersMan.setIdentity("1");
                    usersMan.setRegisterTime(getRanDate());
                }
                usersManList.add(usersMan);
                //usersManMapper.insertSelective(usersMan);

            }


            //=============内容表
            SpsDataContent content = new SpsDataContent();
            content.setId(idWorker.nextId());
            content.setCityId(getArrByRand(integers));

            content.setUpdatedDt(getRanDate());
            content.setSpsPlatformUserId(userId);
            if (yuqingProData.getPublishDate() != null){
                //String publishDate = yuqingProData.getPublishDate();
                content.setCreatedDt(sdf.parse(yuqingProData.getPublishDate()));
            }
            content.setContent(yuqingProData.getContent());
            content.setContentTitle(yuqingProData.getTitle());
            content.setContentUrl(yuqingProData.getSourceUrl());
            content.setLanguage("zh_cn");
            if (yuqingProData.getAuthor() != null && yuqingProData.getAuthor().length() > 0) {
                content.setAuthor(yuqingProData.getAuthor());
            } else {
                content.setAuthor("无");
            }
            if (yuqingProData.getDtailHead() != null && yuqingProData.getDtailHead().length() > 0) {
                if (yuqingProData.getDtailHead().equals("待分类")) {
                    content.setJudge("1");
                } else if (yuqingProData.getDtailHead().equals("无关")) {
                    content.setJudge("0");
                } else if (yuqingProData.getDtailHead().equals("中性")) {
                    content.setJudge("2");
                } else if (yuqingProData.getDtailHead().equals("正面")) {
                    content.setJudge("3");
                } else if (yuqingProData.getDtailHead().equals("负面")) {
                    content.setJudge("4");
                }
            } else {
                content.setJudge("0");
            }
            content.setVisits(getArrByRand(integers2));
            content.setPv(getArrByRand(integers3));
            //String spsPlatformUserId = collectionManMapper.selectIdByPlatformName(yuqingProData.getMediaName());
            if (platformId != null && platformId.length() > 0) {
                content.setBasicPlatformId(Long.valueOf(platformId));
            } else {
                content.setBasicPlatformId(1L);
            }

            //contentMapper.insertSelective(content);
            contentList.add(content);

        }
        int row1 = usersManMapper.insertBatch(usersManList);
        int row = contentMapper.insertSelectiveBatch(contentList);

        result.put("total", yuqingProDataList.size());
        result.put("contentList", contentList.size());
        return CommonResult.success(result);
    }

    /**
     * 获取随机24小时内的时间
     *
     * @return
     */
    private Date getRanDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);//获取当前日期的前一天
        Date startDate = calendar.getTime();
        Calendar cal = Calendar.getInstance();
        Date endDate = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 生成时间格式
        String format1 = sdf.format(startDate);
        String format2 = sdf.format(endDate);
        try {
            long l1 = sdf.parse(format1).getTime();
            long l2 = sdf.parse(format2).getTime();
            long time = (long) ((l2 - l1) * Math.random() + l1);
            String format = sdf.format(time);
            return sdf.parse(format);
        } catch (ParseException e) {
            return new Date();
        }
    }


    /***
     * 随机返回cityId
     * @param list
     * @return
     */
    public int getArrByRand(List<Integer> list) {
        int index = (int) (Math.random() * list.size());
        return list.get(index);
    }


}
